from algorithm import Hand_Algorithm


class Algorithm_wrapper(Hand_Algorithm):
    def __init__(self) -> None:
        super().__init__()

    def main_loop(self):
        while True:
            key_arm = "Left"
            # self.set_hand_data()
            self._dict_hand_landmarks = self.get_data()

            if key_arm in self._dict_hand_landmarks and self._count_arm() == 1:
                key_arm = "Left"
                distance_arm = self._hand_distance_before_screen(key_arm)
                # distance_arms = self.hand_distance()
                print(f"Растояние до руки {distance_arm[0]}")
                angle = self.hand_angle(key_arm)
                print(f"Угол {key_arm} руки {angle}")
                self._rotate_points_90()
                print(self.fingers_bool(key_arm))

            elif self._count_arm() == 2:
                key_arm = "Left"
                distance_arms = self.hand_distance()
                print(f"Растояние от руки до руки {distance_arms}")
                self._rotate_points_90()
                print(self.arms_fingers_bool())
                key_press = self.find_press_button(1)
                key_press_all = self.find_all_press_button(8)
                print(f"Нажата кнопка {key_press}.All нажата кнопка  {key_press_all}")
            self._dict_hand_landmarks = {}

    def arm_cheek_distance(self):
        for name_arm in self._dict_hand_landmarks:
            self._dict_arms_distance[name_arm] = self._hand_distance_before_screen(
                name_arm
            )[0]
        return self._dict_arms_distance

    def arms_fingers_bool(self):
        _dict_arms_fingers_bool = {}
        for name_arm in self._dict_hand_landmarks:
            _dict_arms_fingers_bool[name_arm] = self.fingers_bool(name_arm)
        return _dict_arms_fingers_bool

    def hand_distance_before_screen(self):
        pass

    def get_hand_data(self):
        return self._dict_hand_landmarks

    def set_hand_data(self, handLandmarks={}):
        self._dict_hand_landmarks = handLandmarks
        self._rotate_points_90()

    def find_press_button(
        self, button, fingre=8, arm="Right", radius=40, cheek_distance=True
    ):
        finger_index = -1
        if self._count_arm() == 2:
            for fingre_index in range(0, 21):
                if (
                    self.press_button(button, fingre_index, arm, radius=radius) == True
                    and fingre == fingre_index
                ):
                    return fingre_index
        return finger_index

    def find_all_press_button(self, button, arm="Left", radius=20, cheek_distance=True):
        finger_index = -1
        if self._count_arm() == 2:
            for fingre_index in range(0, 21):
                if self.press_button(button, fingre_index, arm, radius=radius) == True:
                    return fingre_index
        return finger_index

    def hand_distance(self, Left_point=5, Right_point=5):
        Left = self._dict_hand_landmarks["Left"][Left_point]
        Right = self._dict_hand_landmarks["Right"][Right_point]
        Left_x = Left[0]
        Left_y = Left[1]
        Right_x = Right[0]
        Right_y = Right[1]
        return self.length_line(Left_x, Left_y, Right_x, Right_y)

    def press_button(self, button, press_fingre, key_press_arm="Left", radius=20):
        press_arm = self._dict_hand_landmarks[key_press_arm]
        key_button_arm = ""
        button_arm = []
        if key_press_arm == "Left":
            key_button_arm = "Right"
        else:
            key_button_arm = "Left"
        button_arm = self._dict_hand_landmarks[key_button_arm]
        button_point_x = button_arm[button][0]
        button_point_y = button_arm[button][1]
        press_point_x = press_arm[press_fingre][0]
        press_point_y = press_arm[press_fingre][1]
        bool_distance_button_arm = self._cheek_distance(key_button_arm)
        bool_distance_press_arm = self._cheek_distance(key_press_arm)

        if bool_distance_button_arm and bool_distance_press_arm:
            return self._cheek_point_in_radius(
                press_point_x, press_point_y, button_point_x, button_point_y, radius
            )
        else:
            return False
